package com.qin.service.impl;

import com.qin.dao.EmployeeMapper;
import com.qin.entity.Employee;
import com.qin.service.EmployeeService;
import com.qin.utils.PasswordUtil;
import com.qin.utils.SystemConstant;
import com.qin.utils.UUIDUtils;
import com.qin.vo.EmployeeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;


    /**
     * 员工登录
     * @param loginName 登录名称
     * @param loginPwd  登录密码
     * @return
     */
    public Employee login(String loginName, String loginPwd) {
        // 调用根据账号查询员工信息的方法
        Employee employee = employeeMapper.findEmployeeByLoginName(loginName);
        // 判断对象是否为空
        if(employee != null){
            // 将密码加密处理
            String newPassword = PasswordUtil.md5(loginPwd,employee.getSalt(), SystemConstant.PASSWORD_COUNT);
            // 比较密码是否一致
            if(employee.getLoginPwd().equals(newPassword)){
                return employee;    // 登录成功
            }
        }
        // 登录失败
        return null;
    }

    /**
     * 根据部门编号查询员工数量
     * @param deptId
     * @return
     */
    public int getEmployeeCountByDeptId(Integer deptId) {
        return employeeMapper.getEmployeeCountByDeptId(deptId);
    }

    /**
     * 根据角色编号查询员工数量
     * @param roleId
     * @return
     */
    public int getEmployeeCountByRoleId(Integer roleId) {
        return employeeMapper.getEmployeeCountByRoleId(roleId);
    }

    /**
     * 查询员工列表
     * @param employeeVo
     * @return
     */
    public List<Employee> findEmployeeList(EmployeeVo employeeVo) {
        return employeeMapper.findEmployeeList(employeeVo);

    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    public int addEmployee(Employee employee) {
        employee.setSalt(UUIDUtils.randomUUID()); // 设置默认加密盐值
        employee.setCreateDate(new Date()); // 创建时间
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,employee.getSalt(),SystemConstant.PASSWORD_COUNT));//默认密码
        return employeeMapper.addEmployee(employee);
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    public int updateEmoloyee(Employee employee) {
        // 设置修改时间
        employee.setModifyDate(new Date());
        return employeeMapper.updateEmoloyee(employee);
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    public int deleteById(Integer id) {
        // 删除员工关系表的数据
        employeeMapper.deleteEmployeeAndRole(id);
        // 调用删除员工的方法
        return employeeMapper.deleteById(id);
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    public int resetPwd(int id) {
        Employee employee = new Employee();
        // 设置盐值
        employee.setSalt(UUIDUtils.randomUUID());
        // 再给密码重新加密赋值
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,employee.getSalt(),SystemConstant.PASSWORD_COUNT));
        employee.setId(id); // 主键,员工编号
        return employeeMapper.updateEmoloyee(employee);
    }

    /**
     * 保存员工角色关系
     * @param roleIds
     * @param empId
     * @return
     */
    public boolean saveEmployeeRole(String roleIds, Integer empId) {
        try {
            // 先删除员工角色关系表的数据
            employeeMapper.deleteEmployeeAndRole(empId);
            // 再保存员工角色关系
            String[] idStr = roleIds.split(",");
            for (int i = 0; i < idStr.length; i++) {
                employeeMapper.addEmployeeRole(idStr[i],empId);

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}





















